package com.jiazhong.spring.ai.ollama.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ollama_memory_ai")
public class OllamaMemoryController {
    @Resource
    private ChatClient ollamaChatClient;
    private String message = "";

    @GetMapping(value = "/a", produces = "text/html;charset=utf-8")
    public Flux<String> a(@RequestParam("message") String message) {
        this.message += message;
        return ollamaChatClient.prompt().user(this.message)
                .stream()
                .content();
    }

    @GetMapping(value = "/b", produces = "text/html;charset=utf-8")
    public Flux<String> b(@RequestParam("question") String question, @RequestParam("userId") String userId) {
        return ollamaChatClient
                .prompt()
                .advisors(advisor -> advisor.param("chat_memory_conversation_id", userId))
                .user(question)
                .stream()
                .content();
    }
}
